package kr.hkit.score_pjt.ListUI;

import java.awt.BorderLayout;
import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;

import kr.hkit.score_pjt.DTO.StdInfo;

public class ScoreListUI extends JFrame {

	private JPanel contentPane;
	private ArrayList<StdInfo> list;
	private JTable table;
	private CustomModel model;
	
	/**
	 * Create the frame.
	 */
	public ScoreListUI(String title, ArrayList<StdInfo> list) {
		this.list = list;
		initialize(title);
	}
	
	private void initialize(String title) {
		setResizable(false);
		setTitle(title);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 500, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		model = new CustomModel();
		table = new JTable(model);
		scrollPane.setViewportView(table);
		alignData();
		
		/*DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
		dtcr.setHorizontalAlignment(SwingConstants.CENTER);
		
		TableColumnModel alignModel = table.getColumnModel();
		alignModel.getColumn(0).setCellRenderer(dtcr);
		alignModel.getColumn(1).setCellRenderer(dtcr);
		alignModel.getColumn(2).setCellRenderer(dtcr);
		alignModel.getColumn(3).setCellRenderer(dtcr);
		alignModel.getColumn(4).setCellRenderer(dtcr);
		alignModel.getColumn(5).setCellRenderer(dtcr);
		alignModel.getColumn(6).setCellRenderer(dtcr);
		alignModel.getColumn(7).setCellRenderer(dtcr);
		alignModel.getColumn(8).setCellRenderer(dtcr);*/
	}

	private void alignData() {
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		for(int i=0; i<4; i++){
			table.getColumnModel().getColumn(i).setCellRenderer(dtcrCenter);
		}
		
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		
		for(int i=4; i<9; i++){
			table.getColumnModel().getColumn(i).setCellRenderer(dtcrRight);
		}
		

	}

	public void refreshData(ArrayList<StdInfo> list) {
		this.list = list;
		model.fireTableDataChanged();
	}
	
	class CustomModel extends AbstractTableModel{
		final String[] COLUMN_NAME = {"등급", "순위", "학번", "성명", "국어", "영어", "수학", "총점", "평균"};
		final Class[] COLUMN_CLASS = {Integer.class, Integer.class, Integer.class, String.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class };
		
		@Override
		public int getRowCount() {
			return list.size();
		}

		@Override
		public Class<?> getColumnClass(int columnIndex) {
			return COLUMN_CLASS[columnIndex];
		}

		@Override
		public int getColumnCount() {
			return COLUMN_NAME.length;
		}

		@Override
		public String getColumnName(int column) {
			return COLUMN_NAME[column];
		}
		
		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			StdInfo dto = list.get(rowIndex);
			
			switch(columnIndex){
			case 0: return dto.getStdGrade();
			case 1: return dto.getStdRank();
			case 2: return dto.getStdNum();
			case 3: return dto.getStdName();
			case 4: return dto.getKorScore();
			case 5: return dto.getEngScore();
			case 6: return dto.getMathScore();
			case 7: return dto.getTotal();
			case 8: return dto.getAvg();
			}
			return null;
		}
		
	}

}
